<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    ES6 Symbol内置值 - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						ES6 Symbol内置值
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/09/14 20:28 下午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<p>除了定义自己使用的<code>Symbol</code>值以外，<code>ES6</code>提供了11个内置的<code>Symbol</code>值，指向语言内部使用的方法</p>
<h2><a id="symbol-hasinstance" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>Symbol.hasInstance</h2>
<p>当其他对象使用<code>instanceof</code>运算符时，判断是否在该对象的原型链上</p>
<pre class="line-numbers"><code class="language-js">class Person{
    static[Symbol.hasInstance](){
        console.log('检测类型');
    }
}

let o = {};
console.log(o instanceof Person); //输出 false
</code></pre>
<h2><a id="symbol-isconcatspreadable" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>Symbol.isConcatSpreadable</h2>
<p>用于控制值是否可以展开</p>
<pre class="line-numbers"><code class="language-js">const arr1 = [1,2,3];
const arr2 = [4,5,6];
console.log(arr1.concat(arr2)); //输出 {1,2,3,4,5,6}

arr2[Symbol.isConcatSpreadable] = false;
console.log(arr1.concat(arr2)); //输出 {1,2,3,{4,5,6}}
</code></pre>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>